from file_define import JsonFileRead, TextFileRead
from pymysql import connect
import json

# 链接数据库
conn = connect(
    host="localhost",
    port=3306,
    user="root",
    password="root",
    autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
text_file_reader = TextFileRead("2011年1月销售数据.txt")
json_file_reader = JsonFileRead("2011年2月销售数据JSON.txt")


# def addDataToTable():
#     text_data: list = text_file_reader.read_data()
#     json_data: list = json_file_reader.read_data()
#     all_data: list = text_data + json_data
#     conn.select_db("py_sql")
#
#     for item in all_data:
#         sql = f"insert into orders(order_date,order_id,money,province) values ('{item.date}','{item.order_id}',{item.money},'{item.province}')"
#         cursor.execute(sql)
#
#
# addDataToTable()

def getDataInTable():
    conn.select_db("py_sql")
    cursor.execute("select * from orders")
    res = cursor.fetchall()
    column_names = [description[0] for description in cursor.description]
    # for re in res:
    result = [dict(zip(column_names, row)) for row in res]
    for item in result:
        item["order_date"] = item["order_date"].isoformat()

    json_data = json.dumps(result, ensure_ascii=False)
    print(json_data)


getDataInTable()
